﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data; 

namespace Woodlands
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ModuleWindow : Page
    {

        public bool newModuleCode = false;

        public ModuleWindow()
        {
            InitializeComponent();
            DataTable moduleTable = new DataTable();
            DataTable moduleTable2 = new DataTable();
            Database.fillModuleCode(0).Fill(moduleTable);
            Database.fillModuleCode(1).Fill(moduleTable2);
            moduleCode.DataContext = moduleTable;
            moduleCode.SelectedIndex = 0;
            moduleCodeAmend.DataContext = moduleTable2;
            moduleCodeAmend.SelectedIndex = 0;
            moduleCodeArchive.DataContext = moduleTable2;
            moduleCodeArchive.SelectedIndex = 0;
            moduleCodeSearch.DataContext = moduleTable2;
            moduleCodeSearch.SelectedIndex = 0;
            createModuleIDTxt.Text = Module.nextID(moduleTable.Rows[0]["moduleCode"].ToString());
        }

        private void moduleCode_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            ComboBox x = ((ComboBox)e.Source);
            string y = x.SelectedValue.ToString();
            if (y == "New...")
            {
                newModuleCode = true;
                moduleCode.Visibility = System.Windows.Visibility.Hidden;
                newModuleCodeText.Visibility = System.Windows.Visibility.Visible;
            }
            createModuleIDTxt.Text = Module.nextID(y);
        }

        private void newModuleCodeText_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (newModuleCodeText.Text.Length == 3)
            {
                MessageBox.Show("Module codes cannot be longer than 3 characters.");
            }
        }

        private void newModuleCodeText_KeyUp_1(object sender, KeyEventArgs e)
        {
            createModuleIDTxt.Text = Module.nextID(newModuleCodeText.Text);
        }

        private void refreshModuleCodeList()
        {
            moduleCode.SelectionChanged -= moduleCode_SelectionChanged_1;
            moduleCodeAmend.SelectionChanged -= moduleCodeAmend_SelectionChanged_1;
            moduleCodeArchive.SelectionChanged -= moduleCodeArchive_SelectionChanged_1;
            moduleCodeSearch.SelectionChanged -= moduleCodeSearch_SelectionChanged_1;
            DataTable moduleTable = new DataTable();
            Database.fillModuleCode(0).Fill(moduleTable);
            DataTable moduleTable2 = new DataTable();
            Database.fillModuleCode(1).Fill(moduleTable2);
            moduleCode.DataContext = moduleTable;
            moduleCodeAmend.DataContext = moduleTable2;
            moduleCodeArchive.DataContext = moduleTable2;
            moduleCodeSearch.DataContext = moduleTable2;
            moduleCode.SelectedIndex = 0;
            moduleCodeAmend.SelectedIndex = 0;
            moduleCodeArchive.SelectedIndex = 0;
            moduleCodeSearch.SelectedIndex = 0;
            createModuleIDTxt.Text = Module.nextID(moduleTable.Rows[0]["moduleCode"].ToString());
            moduleCode.SelectionChanged += moduleCode_SelectionChanged_1;
            moduleCodeAmend.SelectionChanged += moduleCodeAmend_SelectionChanged_1;
            moduleCodeArchive.SelectionChanged += moduleCodeArchive_SelectionChanged_1;
            moduleCodeSearch.SelectionChanged += moduleCodeSearch_SelectionChanged_1;
            return;
        }

        private async void save_Click(object sender, RoutedEventArgs e)
        {
            save.Content = "Saving...";
            save.IsEnabled = false;
            Module m = new Module();
            m.ModuleID = createModuleIDTxt.Text;
            m.Name = createCourseNameTxT.Text;
            if (moduleCode.SelectedValue.ToString() == "New...")
            {
                m.ModuleCode = newModuleCodeText.Text;
                await Database.newModuleCode(newModuleCodeText.Text);
            }
            else
            {
                m.ModuleCode = moduleCode.SelectedValue.ToString();
            }
            m.ModuleLevel = createModuleLevel.SelectedValue.ToString();
            m.ModulePoints = createModulePoints.Text;
            m.AssessmentOneWeighting = createModuleAss1.Text;
            m.AssessmentTwoWeighting = createModuleAss2.Text;
            await m.create();
            refreshModuleCodeList();
            moduleCode.Visibility = System.Windows.Visibility.Visible;
            newModuleCodeText.Visibility = System.Windows.Visibility.Hidden;
            createCourseNameTxT.Text = "";
            createModuleLevel.SelectedIndex = 0;
            createModulePoints.Text = "";
            createModuleAss1.Text = "";
            createModuleAss2.Text = "";
            save.Content = "Save";
            save.IsEnabled = true;
            MessageBox.Show("New Module added");
        }

        private async void amendGetModuleData()
        {
            Module m = await Module.getModule(moduleCodeAmend.SelectedValue.ToString(), amendModuleIdTxt.Text);
            if (m.Name.Length > 0)
            {
                createCourseNameTxT1.IsEnabled = true;
                amendModuleLevel.IsEnabled = true;
                amendModulePoints.IsEnabled = true;
                amendAss1.IsEnabled = true;
                amendAss2.IsEnabled = true;
                createCourseNameTxT1.Text = m.Name;
                amendModuleLevel.SelectedIndex = (int.Parse(m.ModuleLevel) - 1);
                amendModulePoints.Text = m.ModulePoints;
                amendAss1.Text = m.AssessmentOneWeighting;
                amendAss2.Text = m.AssessmentTwoWeighting;
            }
            else
            {
                createCourseNameTxT1.IsEnabled = false;
                amendModuleLevel.IsEnabled = false;
                amendModulePoints.IsEnabled = false;
                amendAss1.IsEnabled = false;
                amendAss2.IsEnabled = false;
                createCourseNameTxT1.Text = "";
                amendModuleLevel.SelectedIndex = 0;
                amendModulePoints.Text = "";
                amendAss1.Text = "";
                amendAss2.Text = "";
            }
        }

        private void moduleCodeAmend_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (amendModuleIdTxt.Text.Length > 3)
            {
                amendGetModuleData();
            }
            else
            {
                createCourseNameTxT1.IsEnabled = false;
                amendModuleLevel.IsEnabled = false;
                amendModulePoints.IsEnabled = false;
                amendAss1.IsEnabled = false;
                amendAss2.IsEnabled = false;
                createCourseNameTxT1.Text = "";
                amendModuleLevel.SelectedIndex = 0;
                amendModulePoints.Text = "";
                amendAss1.Text = "";
                amendAss2.Text = "";
            }
        }

        private void amendModuleIdTxt_KeyUp_1(object sender, KeyEventArgs e)
        {
            if (amendModuleIdTxt.Text.Length > 3)
            {
                amendGetModuleData();
            }
            else
            {
                createCourseNameTxT1.IsEnabled = false;
                amendModuleLevel.IsEnabled = false;
                amendModulePoints.IsEnabled = false;
                amendAss1.IsEnabled = false;
                amendAss2.IsEnabled = false;
                createCourseNameTxT1.Text = "";
                amendModuleLevel.SelectedIndex = 0;
                amendModulePoints.Text = "";
                amendAss1.Text = "";
                amendAss2.Text = "";
            }
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            moduleCode.Visibility = System.Windows.Visibility.Visible;
            newModuleCodeText.Visibility = System.Windows.Visibility.Hidden;
            createCourseNameTxT.Text = "";
            moduleCode.SelectedIndex = 0;
            createModuleLevel.SelectedIndex = 0;
            createModulePoints.Text = "";
            createModuleAss1.Text = "";
            createModuleAss2.Text = "";
        }

        private void cancel_Copy_Click(object sender, RoutedEventArgs e)
        {
            createCourseNameTxT1.IsEnabled = false;
            amendModuleLevel.IsEnabled = false;
            amendModulePoints.IsEnabled = false;
            amendAss1.IsEnabled = false;
            amendAss2.IsEnabled = false;
            createCourseNameTxT1.Text = "";
            amendModuleIdTxt.Text = "";
            amendModuleLevel.SelectedIndex = 0;
            amendModulePoints.Text = "";
            amendAss1.Text = "";
            amendAss2.Text = "";
        }

        private async void save_Copy_Click(object sender, RoutedEventArgs e)
        {
            save_Copy.Content = "Saving...";
            save_Copy.IsEnabled = false;
            Module m = new Module();
            m.ModuleCode = moduleCodeAmend.SelectedValue.ToString();
            m.ModuleID = amendModuleIdTxt.Text;
            m.Name = createCourseNameTxT1.Text;
            m.ModuleLevel = (amendModuleLevel.SelectedIndex + 1).ToString();
            m.ModulePoints = amendModulePoints.Text;
            m.AssessmentOneWeighting = amendAss1.Text;
            m.AssessmentTwoWeighting = amendAss2.Text;
            await m.amend();
            moduleCodeAmend.SelectedIndex = 0;
            amendModuleIdTxt.Text = "";
            createCourseNameTxT1.IsEnabled = false;
            amendModuleLevel.IsEnabled = false;
            amendModulePoints.IsEnabled = false;
            amendAss1.IsEnabled = false;
            amendAss2.IsEnabled = false;
            createCourseNameTxT1.Text = "";
            amendModuleLevel.SelectedIndex = 0;
            amendModulePoints.Text = "";
            amendAss1.Text = "";
            amendAss2.Text = "";
            save_Copy.Content = "Save";
            MessageBox.Show("Amend completed");
            save_Copy.IsEnabled = true;
        }

        private async void displayAchive()
        {
            Module m = await Module.getModule(moduleCodeArchive.SelectedValue.ToString(), createCourseIDTxt2.Text);
            if (m.Name == "")
            {
                currentStatusTxtArchive.Text = "Please Enter a valid module...";
            }
            else
            {
                if (m.Status == "False")
                {
                    createCourseNameTxT2.Text = m.Name;
                    currentStatusTxtArchive.Text = "Achived";
                }
                else if (m.Status == "True")
                {
                    createCourseNameTxT2.Text = m.Name;
                    currentStatusTxtArchive.Text = "Live";
                }
            }
        }

        private void moduleCodeArchive_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (createCourseIDTxt2.Text.Length > 3)
            {
                displayAchive();
            }
            else
            {
                currentStatusTxtArchive.Text = "Please Enter a valid module...";
                createCourseNameTxT2.Text = "";
            }
        }

        private void createCourseIDTxt2_KeyUp_1(object sender, KeyEventArgs e)
        {
            if (createCourseIDTxt2.Text.Length > 3)
            {
                displayAchive();
            }
            else
            {
                currentStatusTxtArchive.Text = "Please Enter a valid module...";
                createCourseNameTxT2.Text = "";
            }
        }

        private async void BtnArchive_Click(object sender, RoutedEventArgs e)
        {
            if (currentStatusTxtArchive.Text == "Live")
            {
                await Module.achive(moduleCodeArchive.SelectedValue.ToString(), createCourseIDTxt2.Text);
                currentStatusTxtArchive.Text = "Archived";
            }
            else
            {
                MessageBox.Show("Module must be live to be archived");
            }
        }

        private void CancelBtnArchive_Click(object sender, RoutedEventArgs e)
        {
            moduleCodeArchive.SelectedIndex = 0;
            createCourseIDTxt2.Text = "";
            currentStatusTxtArchive.Text = "Please Enter a valid module...";
            createCourseNameTxT2.Text = "";
        }
        public string DialogValue = "";
        private void moduleCodeSearch_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (resultsBox.SelectedValue != null && resultsBox.SelectedIndex != 0)
            {
                ModuleDialogBox dialog = new ModuleDialogBox(this);
                dialog.ShowDialog();
                if (DialogValue == "amend")
                {
                    amendModuleIdTxt.Text = resultsBox.SelectedValue.ToString().Substring(1, 5);
                    amendGetModuleData();
                    subMenu.SelectedIndex = 1;
                }
                else if (DialogValue == "archive")
                {
                    createCourseIDTxt2.Text = resultsBox.SelectedValue.ToString().Substring(1, 5);
                    displayAchive();
                    subMenu.SelectedIndex = 2;
                }
                else
                {
                    resultsBox.SelectedItem = null;
                }
                DialogValue = "";
            }
        }

        private async void moduleSearch_KeyUp_1(object sender, KeyEventArgs e)
        {
            resultsBox.Items.Clear();
            if (moduleSearch.Text.Length > 0)
            {
                if (searchType.SelectedIndex == 0)
                {
                    resultsBox.Items.Add("ModuleID\tModule Name");
                    DataTable dt = await Module.search("ID", moduleCodeSearch.SelectedValue.ToString() + moduleSearch.Text);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string data = dt.Rows[i]["moduleID"].ToString() + "\t\t" + dt.Rows[i]["name"].ToString();
                        resultsBox.Items.Add(data);
                    }
                }
                else if (searchType.SelectedIndex == 1)
                {
                    resultsBox.Items.Add("ModuleID\tModule Name");
                    DataTable dt = await Module.search("Name", moduleSearch.Text);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string data = dt.Rows[i]["moduleID"].ToString() + "\t\t" + dt.Rows[i]["name"].ToString();
                        resultsBox.Items.Add(data);
                    }
                }
            }
        }

        private void searchType_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (searchType.SelectedIndex > 0)
            {
                moduleCodeSearch.IsEnabled = false;
            }
            else
            {
                try
                {
                    moduleCodeSearch.IsEnabled = true;
                }
                catch (NullReferenceException ex)
                {
                    //Catches an odd NullReferenceException
                }
            }
        }

    }
}
